/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.gis.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.gis.domain.Transportation;
import me.zhengjie.modules.gis.service.TransportationService;
import me.zhengjie.modules.gis.service.dto.TransportationQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author ZHUANGBING YAN
* @date 2022-09-08
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "路径数据管理")
@RequestMapping("/api/transportation")
public class TransportationController {

    private final TransportationService transportationService;

    @AnonymousAccess
    @GetMapping("/getLine/{id}/{positionS}/{positionE}")
    @Log("查询路径数据")
    @ApiOperation("查询路径数据")
    public ResponseEntity<Object> getLine(@PathVariable("id")Long Id,@PathVariable("positionS") String positionS,
                                          @PathVariable("positionE") String positionE){
        return new ResponseEntity<>(transportationService.getLine( Id,positionS,positionE),HttpStatus.OK);
    }


    @GetMapping("/queryAll")
    @Log("查询路径数据")
    @ApiOperation("查询路径数据")
    public ResponseEntity<Object> queryAll(Boolean isDel){
        return new ResponseEntity<>(transportationService.listAll( isDel),HttpStatus.OK);
    }

    @GetMapping("/listAll")
    @Log("查询路径数据")
    @ApiOperation("查询路径数据")
    public ResponseEntity<Object> listAll(Boolean type){
        return new ResponseEntity<>(transportationService.selectAll(type),HttpStatus.OK);
    }

    @GetMapping("/queryRandom")
    @Log("查询路径数据")
    @ApiOperation("查询路径数据")
    public ResponseEntity<Object> queryAll(){
        return new ResponseEntity<>(transportationService.queryRandom(),HttpStatus.OK);
    }

    @GetMapping("/queryErrorRandom")
    @Log("查询路径数据")
    @ApiOperation("查询路径数据")
    public ResponseEntity<Object> queryErrorRandom(){
        return new ResponseEntity<>(transportationService.queryErrorRandom(),HttpStatus.OK);
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('transportation:list')")
    public void exportTransportation(HttpServletResponse response, TransportationQueryCriteria criteria) throws IOException {
        transportationService.download(transportationService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询路径数据")
    @ApiOperation("查询路径数据")
    @PreAuthorize("@el.check('transportation:list')")
    public ResponseEntity<Object> queryTransportation(TransportationQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(transportationService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增路径数据")
    @ApiOperation("新增路径数据")
    @PreAuthorize("@el.check('transportation:add')")
    public ResponseEntity<Object> createTransportation(@Validated @RequestBody Transportation resources){
        return new ResponseEntity<>(transportationService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改路径数据")
    @ApiOperation("修改路径数据")
    @PreAuthorize("@el.check('transportation:edit')")
    public ResponseEntity<Object> updateTransportation(@Validated @RequestBody Transportation resources){
        transportationService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除路径数据")
    @ApiOperation("删除路径数据")
    @PreAuthorize("@el.check('transportation:del')")
    public ResponseEntity<Object> deleteTransportation(@RequestBody Long[] ids) {
        transportationService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}